'use strict';
const json = require('koa-json');
const onerror = require('koa-onerror');
const logger = require('koa-logger');
const koaBody = require('koa-body');
const cors = require('koa2-cors'); //设置允许跨域插件
module.exports = app => {
  onerror(app);
  // middlewares
  app.use(koaBody());
  app.use(cors()); //设置允许跨域
  app.use(json());
  app.use(logger());
  // app.use(require('koa-static')(__dirname + '/public'));
  app.use(require('koa-static')(__dirname + '../app/public'));

  // logger
  app.use(async (ctx, next) => {
    const start = new Date();
    await next();
    const ms = new Date() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
  });
  // error-handling
  app.on('error', (err, ctx) => {
    console.error('server error', err, ctx);
  });
};
